package com.flashflexpro.flexdirector.adapter.codec {
import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;
import com.flashflexpro.flexdirector.model.CodeStringValue;

import mx.automation.IAutomationObject;

public class CodecColor extends CodecDefault{

    override public function encode(obj:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject ):CodeStringValue{
        var rt:CodeStringValue = new CodeStringValue();
        rt.desc = descProperty.getDto();
        rt.value = getMemberFromObject( obj, descProperty);

        if ( rt.value != null){
            var str:String = Number(rt.value).toString(16);
            while (str.length != 6){
                str = "0" + str;
            }
            str = "0x" + str;
        }
        rt.code = rt.string = str;
        return rt;
    }

    override public function decode(obj:Object, value:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject, inString:Boolean = false):void {
        if( inString ){
            obj[descProperty.name] = parseInt(String(value).substring(2), 16);
        }
        else{
            obj[descProperty.name] = value;
        }
    }
}
}